/* Copyright (c) 2013, Shawn McGregor and Steve Mitchell All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. AT30TS75x.c Notes: */ #include "AT30TS75x.h" #include #include /****************************************************************** Function: int AT30TS75xRead(unsigned int w_register) Input: Output: Overview: ********************************************************************/ int AT30TS75xRead(unsigned int w_register) { unsigned char datahi = 0; unsigned char datalo = 0; unsigned int result = 0; IdleI2C(); StartI2C(); while(SSPCON2bits.SEN); // wait till start condition is over WriteI2C( AT30TS75x_Write ); IdleI2C(); WriteI2C( w_register ); // set selected register pointer value StopI2C(); while ( SSPCON2bits.PEN ); // wait until stop condition is over IdleI2C(); StartI2C(); while ( SSPCON2bits.SEN ); // wait until start condition is over WriteI2C( AT30TS75x_Read ); // read upper byte of data IdleI2C(); datahi = ReadI2C(); AckI2C(); WriteI2C( AT30TS75x_Read ); // read lower byte of data IdleI2C(); datalo = ReadI2C(); NotAckI2C(); while ( SSPCON2bits.ACKEN ); // wait until NACK sequence is over StopI2C(); while ( SSPCON2bits.PEN ); // wait until stop condition is over result = datahi; result = result << 8; // move upper byte into position result = result + datalo; // move lower byte into position return (result); } /****************************************************************** Function: int AT30TS75xWrite(unsigned int w_register, unsigned int w_value) Input: Output: Overview: ********************************************************************/ int AT30TS75xWrite(unsigned int w_register, unsigned int w_value) { unsigned char datahi = 0; unsigned char datalo = 0; datahi = w_value >> 8; // seperate into upper and lower bytes datalo = (char) w_value; IdleI2C(); StartI2C(); while(SSPCON2bits.SEN); // wait till start condition is over WriteI2C( AT30TS75x_Write ); IdleI2C(); WriteI2C( w_register ); // set selected register pointer value IdleI2C(); WriteI2C( datahi ); // write to the upper byte of register IdleI2C(); WriteI2C( datalo ); // write to the lower byte of register IdleI2C(); StopI2C(); while ( SSPCON2bits.PEN ); // wait until stop condition is over return (0); }